<?php 
class CNotifyinfo extends Zend_Db_Table
{
	protected $_name;
	protected $_primary;
	protected $_apiDomain = 'http://www.gogoalshop.com/';
	
	public function __construct()
	{
		$this->_name=R_PRE. "notify_info";
		parent::__construct();
	}
	
	public function updateProduct($oldItemid)
	{
		$db = $this->getAdapter();
		$str = file_get_contents($this->_apiDomain . 'productapi?id=' . $oldItemid);
		$arr = json_decode($str, true);
		
		//item
		$itemRow = $db->fetchRow('select * from zcshop_items where fromitemid=' . $oldItemid);
		$item = new CItem();
		$itemId = $itemRow['itemid'];
		$data = $arr['item'];
		unset($data['itemid']);
		unset($data['fromitemid']);
		$interArr = explode(',', $data['interrelated']);
		$realInterArr = array();
		if ($interArr) {
			foreach ($interArr as $_v) {
				$tmpId = 0;
				$tmpId = $this->getRealItemid($_v);
				$realInterArr[] = $tmpId;
			}
		}
		$data['interrelated'] = implode(',', $realInterArr);
		$item->editItem($itemId, $data);
		
		//product
		$productRow = $arr['product'];
		$product = new CProduct();
		unset($productRow['itemid']);
		unset($productRow['productid']);
		$productRow['m_org_price'] = $productRow['max_price'] + 40;
		$this->checkGetFile($productRow['cut_url']);
		$this->checkGetFile($productRow['medium_url']);
		$product->editProduct($itemId, $productRow);
		
		//price
		$price = $arr['price'];
		$productPrice = new CProductPrice();
		//删除老的
		$productPrice->delPricesByItemid(array($itemId));
		//增加新的
		foreach ($price as $v) {
			unset($v['priceid']);
			$v['itemid'] = $itemId;
			$productPrice->createPrice($v);
		}
		
		//attachments
		$attachments = $arr['attachments'];
		$productAttachments = new CAttachment();
		//删除原来的
		try {
			$productAttachments->delAttachmentsByItemid(array($itemId));
		} catch (Exception $e) {
			
		}
		foreach ($attachments as $v) {
			unset($v['attachmentid']);
			$v['itemid'] = $itemId;
			$this->checkGetFile($v['attachment_url']);
			$this->checkGetFile($v['cut_url']);
			$this->checkGetFile($v['medium_img']);
			$productAttachments->createAttachments(array($v));
		}
		
		//badge
		$badge = $arr['badge'];
		//删除
		$db = $productPrice->getAdapter();
		$db->query('delete from zcshop_badge_product where itemid=' . $itemId);
		foreach ($badge as $v) {
			$v['itemid'] = $itemId;
			$db->query('insert into zcshop_badge_product(badge_id, itemid) values('.$v['badge_id'].', '.$v['itemid'].')');
		}
		
		//product attr
		$productAttr = $arr['attr'];
		$productAttrModel = new CProductattr();
		$productAttrModel->delete('itemid=' . $itemId);
		foreach ($productAttr as $v) {
			unset($v['attr_id']);
			$v['itemid'] = $itemId;
			$productAttrModel->insert($v);
		}
		//accessory
		$accrssoryArr = $arr['accessory'];
		$db->query('delete from zcshop_product_accessory where itemid=' . $itemId);
		foreach ($accrssoryArr as $v) {
			$v['itemid'] = $itemId;
			$v['accessoryid'] = $this->getRealItemid($v['accessoryid']);
			if ($v['accessoryid']) {
				$db->query('insert into zcshop_product_accessory(itemid, accessoryid,price, flid) values('.$v['itemid'].', '.$v['accessoryid'].',' . $v['price'] . ', '.$v['flid'].')');
			}
		}
		echo 'ok';
	}
	
	public function addProduct($oldItemid)
	{
		$db = $this->getAdapter();
		$str = file_get_contents($this->_apiDomain . 'productapi?id=' . $oldItemid);
		$arr = json_decode($str, true);
		
		//item		
		$item = new CItem();		
		$data = $arr['item'];
		unset($data['itemid']);
		$interArr = explode(',', $data['interrelated']);
		$realInterArr = array();
		if ($interArr) {
			foreach ($interArr as $_v) {
				$tmpId = 0;
				$tmpId = $this->getRealItemid($_v);
				$realInterArr[] = $tmpId;
			}
		}
		$data['interrelated'] = implode(',', $realInterArr);
		$data['fromitemid'] = $oldItemid;
		$itemId = $item->createItem($data);
		
		//product
		$productRow = $arr['product'];
		$product = new CProduct();
		$productRow['itemid'] = $itemId;	
		unset($productRow['productid']);
		$productRow['m_org_price'] = $productRow['max_price'] + 40;
		$this->checkGetFile($productRow['cut_url']);
		$this->checkGetFile($productRow['medium_url']);	
		$product->createProduct($productRow);
		
		//price
		$price = $arr['price'];
		$productPrice = new CProductPrice();		
		//增加新的
		foreach ($price as $v) {
			unset($v['priceid']);
			$v['itemid'] = $itemId;
			$productPrice->createPrice($v);
		}
		
		//attachments
		$attachments = $arr['attachments'];
		$productAttachments = new CAttachment();		
		foreach ($attachments as $v) {
			unset($v['attachmentid']);
			$v['itemid'] = $itemId;
			$this->checkGetFile($v['attachment_url']);
			$this->checkGetFile($v['cut_url']);
			$this->checkGetFile($v['medium_img']);
			$productAttachments->createAttachments(array($v));
		}
		
		//badge
		$badge = $arr['badge'];
		//删除
		$db = $productPrice->getAdapter();		
		foreach ($badge as $v) {
			$v['itemid'] = $itemId;
			$db->query('insert into zcshop_badge_product(badge_id, itemid) values('.$v['badge_id'].', '.$v['itemid'].')');
		}
		
		//product attr
		$productAttr = $arr['attr'];
		$productAttrModel = new CProductattr();		
		foreach ($productAttr as $v) {
			unset($v['attr_id']);
			$v['itemid'] = $itemId;
			$productAttrModel->insert($v);
		}
		
		//accessory
		$accrssoryArr = $arr['accessory'];		
		foreach ($accrssoryArr as $v) {
			$v['itemid'] = $itemId;
			$v['accessoryid'] = $this->getRealItemid($v['accessoryid']);
			if ($v['accessoryid']) {
				$db->query('insert into zcshop_product_accessory(itemid, accessoryid,price, flid) values('.$v['itemid'].', '.$v['accessoryid'].',' . $v['price'] . ', '.$v['flid'].')');
			}
		}
		
		echo 'ok';
	}
	
	public function checkGetFile($file)
	{
		return;
		$root = MY_ROOT;		
		$tmpImg = explode('?', $file);
	    $arr = pathinfo($tmpImg[0]);
	    $dirName = $root . '/' . $arr['dirname'];
	    if (!file_exists($dirName)) {
	        mkdir($dirName, 0777, true);
	    }
	    if (!file_exists($root . '/' . $file)) {
	    	$content = file_get_contents($this->_apiDomain . $file);
	    	@file_put_contents($root . '/' . $file,$content);
	    }
	    return;
	}
	
	public function other()
	{
		
		//attr
		$str = file_get_contents($this->_apiDomain . 'productapi-other?type=attr');
		$arr = json_decode($str, true);
		
		$attr = $arr;
		$attrModel = new CAttr();
		$attrIds = array();
		foreach ($attr as $v) {
			$attrIds[] = $v['attr_id'];
			if ($row = $attrModel->fetchRow(array('attr_id = ?'=>$v['attr_id']))) {
				$oldAttrId= $v['attr_id'];
				unset($v['attr_id']);
				$attrModel->update($v, 'attr_id=' . $oldAttrId);
			} else {
				$attrModel->add($v);
			}
		}
		
		$str = file_get_contents($this->_apiDomain . 'productapi-other?type=badge');
		$arr = json_decode($str, true);
		$badge = $arr;
		$badgeModel = new CBadge();
		$badgeIds = array();
		foreach ($badge as $v) {
			$badgeIds[] = $v['badge_id'];
			if ($row = $badgeModel->fetchRow(array('badge_id = ?'=>$v['badge_id']))) {
				$badgeId = $v['badge_id'];
				unset($v['badge_id']);
				$this->checkGetFile($v['img']);
				$badgeModel->update($v, 'badge_id=' . $badgeId);
			} else {
				$this->checkGetFile($v['img']);
				$badgeModel->insert($v);
			}
		}
		
		$str = file_get_contents($this->_apiDomain . 'productapi-other?type=fls');
		$arr = json_decode($str, true);
		$fls = $arr;
		$productfls = new CFenlei('product');
		$flsIds = array();
		foreach ($fls as $v) {
			$flsIds[] = $v['flid'];
			if ($row = $productfls->fetchRow(array('flid = ?'=>$v['flid']))) {
				$flid = $v['flid'];
				unset($v['flid']);
				$productfls->update($v, 'flid=' . $flid);
			} else {
				$productfls->insert($v);
			}
		}
		
		$str = file_get_contents($this->_apiDomain . 'productapi-other?type=lagnappe');
		$arr = json_decode($str, true);
		$lagnappe = $arr;
		$db = $this->getAdapter();
		$lagnappeModel = new CLagnappe();
		$lagnappeAttachemnts = new CLagnappeAttachment();
		$lagnappeIds = array();
		foreach ($lagnappe as $v) {
			$lagnappeIds[] = $v['pid'];
			if ($row = $lagnappeModel->fetchRow(array('pid = ?'=>$v['pid']))) {
				$pid = $v['pid'];
				unset($v['pid']);
				$this->checkGetFile($v['cut_url']);
				$lagnappeModel->update($v, 'pid=' . $pid);
			} else {
				$this->checkGetFile($v['cut_url']);
				$lagnappeModel->insert($v);
			}
		}
		
		$str = file_get_contents($this->_apiDomain . 'productapi-other?type=lagnappeattachments');
		$arr = json_decode($str, true);
		$attachments = $arr;
		$attachmentsIds = array();
		foreach ($attachments as $v) {
			$attachmentsIds[] = $v['attachmentid'];
			if ($row = $lagnappeAttachemnts->fetchRow(array('attachmentid = ?'=>$v['attachmentid']))) {
				$attachmentid = $v['attachmentid'];
				unset($v['attachmentid']);
				$this->checkGetFile($v['cut_url']);
				$this->checkGetFile($v['attachment_url']);
				$lagnappeAttachemnts->update($v, 'attachmentid=' . $attachmentid);
			} else {
				$this->checkGetFile($v['cut_url']);
				$this->checkGetFile($v['attachment_url']);
				$lagnappeAttachemnts->insert($v);
			}
		}
		
		//配件表
		$db->query('truncate table zcshop_product_accessory');
		$acc = file_get_contents($this->_apiDomain . 'productapi-other?type=acc');
		$arr = json_decode($acc, true);
		$accModel = new CProductaccessory();
		foreach ($arr as $v) {
			if ($v['itemid']) {
				$v['itemid'] = $this->getRealItemid($v['itemid']);
			}
			if ($v['accessoryid']) {
				$v['accessoryid'] = $this->getRealItemid($v['accessoryid']);
			}
			$accModel->insert($v);
		}
		
		//删除多余的
		if ($attrIds) {
			$db->query('delete from zcshop_attr where attr_id not in (' . implode(',', $attrIds) . ')');
		}
		if ($badgeIds) {
			$db->query('delete from zcshop_badge where badge_id not in (' . implode(',', $badgeIds) . ')');
		}
		if ($flsIds) {
			$db->query('delete from zcshop_productfls where flid not in (' . implode(',', $flsIds) . ')');
		}
		if ($lagnappeIds) {
			$db->query('delete from zcshop_lagnappe where pid not in (' . implode(',', $lagnappeIds) . ')');
		}
		if ($attachmentsIds) {
			$db->query('delete from zcshop_lagnappe_attachments where attachmentid not in (' . implode(',', $attachmentsIds) . ')');
		}
	}
	
	public function getRealItemid($fromItemId) 
	{
		if ($fromItemId) {
			$db = $this->getAdapter();
			$itemRow = $db->fetchRow('select * from zcshop_items where fromitemid=' . $fromItemId);
			if ($itemRow) {
				return $itemRow['itemid'];
			}
		}
		return 0;
	}
}